﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard Class="mb-4">
    <DataTableCard TModel="AlmanacArticleEditModel" Headers="_headers" DataSource="Model" SortByItem="@nameof(AlmanacEntryEditModel.Priority)" Title="@($"{_name}列表")" OnClickAdd="AddItem" @ref="dataTableCard" Rounded
                   Predicate="s => dataTableCard==null ||string.IsNullOrWhiteSpace(dataTableCard._search) || (s.ArticleName!=null&&s.ArticleName.Contains(dataTableCard._search))">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 170px;">
                    <MIcon Color="success"
                           Class="mr-2"
                           OnClick="()=>EditItem(context.Item)">@IconType.Edit.ToIconString()</MIcon>
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,true)">@IconType.Up.ToIconString()</MIcon>
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,false)">@IconType.Down.ToIconString()</MIcon>
                    @if (context.Item.Hide)
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }
                    <MIcon Color="error"
                           Class="mr-2"
                           OnClick="()=>DeleteItem(context.Item)">@IconType.Delete.ToIconString()</MIcon>
                </div>
            }
            else if (context.Header.Value == nameof(AlmanacArticleEditModel.Image))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Users.Ranks.ImageChip Image="@context.Item.Image" />
            }
            else if (context.Header.Value == nameof(AlmanacArticleEditModel.Hide))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.StateChip IsHidden="context.Item.Hide" />
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>
</MDCard>

<MDialog @bind-Value="_editDialog"
         MaxWidth="800">
    <EditCard Items="Model" OnEdited="OnEdited" @ref="editCard" Model="_editedItem" ArticleItems="ArticleItems" />
</MDialog>
@code {

    [Parameter]
    public List<AlmanacArticleEditModel> Model { get; set; }
    [Parameter]
    public List<string> ArticleItems { get; set; }

    private string _name = "文章";

    private List<DataTableHeader<AlmanacArticleEditModel>> _headers = new List<DataTableHeader<AlmanacArticleEditModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(AlmanacArticleEditModel.Id)
          },
          new (){ Text= "图片", Value= nameof(AlmanacArticleEditModel.Image)},
          new (){ Text= "词条", Value= nameof(AlmanacArticleEditModel.ArticleName)},
          new (){ Text= "优先级", Value= nameof(AlmanacArticleEditModel.Priority)},
          new (){ Text= "状态", Value= nameof(AlmanacArticleEditModel.Hide)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<AlmanacArticleEditModel> dataTableCard;

    EditCard editCard;
    private bool _editDialog;
    private long _editedItemId;
    private AlmanacArticleEditModel _editedItem;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public void EditItem(AlmanacArticleEditModel item)
    {
        _editedItemId = item.Id;
        _editDialog = true;
        _editedItem = item;
        StateHasChanged();
    }

    public void OnEdited()
    {
        _editDialog = false;
        StateHasChanged();
        if (dataTableCard != null)
        {
            dataTableCard.GetDataFromApi();
        }
    }

    public void AddItem()
    {
        _editedItemId = 0;
        _editDialog = true;
        _editedItem = new AlmanacArticleEditModel();
        StateHasChanged();
    }

    public void DeleteItem(AlmanacArticleEditModel model)
    {
        Model.Remove(model);
        dataTableCard.GetDataFromApi();
    }

    public void EditItemPriority(AlmanacArticleEditModel model, bool up)
    {
        model.Priority += up ? 1 : -1;
        dataTableCard.GetDataFromApi();
    }
    public void HideItem(AlmanacArticleEditModel model, bool hide)
    {
        model.Hide = hide;
        dataTableCard.GetDataFromApi();
    }
}
